package com.car.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Page<T> {
    private Integer currentPage;//表示当前页码数,从页面传递过来的
    private Integer pageSize;//每页显示的固定数据条数
    private List<T> list;//每页显示的具体数据
    private Integer totalCount;//参与分页总的数据条数
    private Integer totalPage;//计算出来的总的页数
    private Integer firstPage;//页面显示的第一个页码数字
    private Integer endPage;//页面显示的最后一个页码数字
    private Integer lastPage;//上一页
    private Integer nextPage;//下一页

    //总页数
    public Integer getTotalCount() {
        totalPage = (totalCount % pageSize == 0) ?
                totalCount / pageSize : totalCount / pageSize + 1;
        return totalPage;
    }

    //首页
    public Integer getFirstPage() {
        if (totalPage <= 10) {
            firstPage = 1;
            endPage = totalPage;
        } else {
            if (currentPage - 5 <= 1) {
                firstPage = 1;
                endPage = 10;
            } else if (currentPage - 5 > 1 && currentPage + 4 < totalPage) {
                firstPage = currentPage - 5;
                endPage = currentPage + 4;
            } else if (currentPage + 4 >= totalPage) {
                firstPage = totalPage - 9;
                endPage = totalPage;
            }
        }
        return firstPage;
    }
    //上一页
    public Integer getLastPage() {
        if (currentPage == 1) {
            lastPage = 1;
        } else {
            lastPage = currentPage - 1;
        }
        return lastPage;
    }

    //下一页
    public Integer getNextPage() {
        if (currentPage == totalPage) {
            nextPage = totalPage;
        } else {
            nextPage = currentPage + 1;
        }
        return nextPage;
    }

}
